package com.ynby.newyp.controller;

import com.ynby.newyp.pojo.CzgcPojo;
import com.ynby.newyp.service.CzgcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

@Controller
public class CzgcController {
    @Autowired
    CzgcService czgcService;

    @InitBinder
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        CustomDateEditor editor = new CustomDateEditor(df, true);//true表示允许为空，false反之
        binder.registerCustomEditor(Date.class, editor);
    }

    @RequestMapping("czgcPage.do")
    public String czgcPage(HttpServletRequest request) {
        HttpSession session = request.getSession();
        if (session.getAttribute("flag") == null) {
            return "page-login";
        }
        return "czgc";
    }

    @RequestMapping("query1.do")
    @ResponseBody
    public List<CzgcPojo> query(CzgcPojo czgcPojo) {
        CzgcPojo as = czgcPojo;
        return czgcService.query(czgcPojo);
    }

    @RequestMapping("index1.do")
    @ResponseBody
    public String add(CzgcPojo czgcPojo) {
//    if ("".equals(arrPojo.getPnum())||arrPojo.getPnum()==null){
//         return "请输入项目编号";
//        }
        System.out.println(czgcPojo);
        return czgcService.add(czgcPojo);
    }
}
